// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: ARZ Setup Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 40 : TYPE_ARZSETUP

// Function declarations
reserve function ARZSetup_Function97
reserve function ARZSetup_Function98
reserve function ARZSetup_Function99
reserve function ARZSetup_Function100
reserve function ARZSetup_Function101
reserve function ARZSetup_Function102

// Static Values
public value ARZSetup_value33 = 0;

// Tables
private table ARZSetup_table0
	0x6C0000, 0x37D0000, 0, 68, 8, 68, 56, 1, 40, 4, 8, 6, 0, 26, 8, 37, 0, 2, 8, 13, 0, 28, 48, 1, 32, 10, 36, 3, 32, 15, 40, 5, 8, 34, 56, 1, 40, 5, 8, 170, 0, 2, 2, 8, 6, 1, 4, 15, 0, 18, 8, 81, 0, 53, 48, 1, 32, 6, 36, 23, 0, 57, 8, 52, 56, 1, 40, 2, 8, 43, 0, 76, 48, 1, 32, 17, 40, 39, 8, 32, 10, 2, 2, 15, 0, 35, 48, 1, 32, 45, 0, 33, 48, 1, 32, 52, 36, 3, 4, 25, 0, 5, 48, 1, 32, 1, 40, 96, 32, 1, 36, 2, 4, 13, 0, 27, 8, 7, 56, 1, 40, 22, 8, 11, 0, 1, 4, 10, 0, 10, 8, 85, 0, 8, 8, 11, 56, 1, 40, 4, 8, 23, 0, 66, 8, 5, 56, 1, 40, 19, 8, 84, 56, 1
end table

private table ARZSetup_table1
	0x6C0000, 0x3810000, 0, 18, 8, 63, 56, 1, 40, 5, 8, 85, 56, 1, 40, 1, 32, 16, 40, 7, 8, 175, 56, 1, 40, 48, 8, 15, 56, 1, 40, 45, 8, 97, 10, 3, 2, 39, 0, 4, 8, 108, 56, 1, 40, 46, 8, 199, 56, 1, 40, 59, 8, 3, 10, 4, 2, 4, 10, 11, 8, 2, 0, 1, 2, 28, 8, 214, 10, 2, 2, 77, 0, 1, 4, 20, 0, 65, 8, 22, 56, 1, 40, 2, 8, 161, 56, 1, 40, 12, 8, 43, 56, 1, 40, 8, 8, 8, 56, 1, 40, 7, 8, 4, 56, 1, 40, 4, 8, 4, 56, 1, 40, 6, 8, 3, 56, 1, 40, 6, 8, 3, 56, 1, 40, 6, 8, 3, 56, 1, 40, 6, 8, 3, 56, 1, 40, 5
end table

private table ARZSetup_table2
	0x6C0000, 0x37D0000, 0, 50, 8, 66, 56, 1, 40, 5, 8, 86, 56, 1, 40, 6, 8, 216, 56, 1, 40, 14, 8, 23, 56, 1, 40, 18, 8, 116, 10, 2, 2, 5, 0, 3, 8, 144, 56, 1, 40, 23, 8, 5, 56, 1, 40, 127, 0, 11, 2, 13, 50, 1, 34, 3, 0, 5, 8, 96, 10, 1, 2, 8, 10, 2, 8, 159, 56, 1, 40, 18, 8, 5, 56, 1, 40, 57, 8, 1, 0, 10, 2, 17, 50, 1, 34, 3, 0, 6, 8, 40, 0, 19, 8, 27, 56, 1, 40, 11, 8, 6, 56, 1, 40, 66, 41, 6, 33, 14, 1, 22, 0, 107, 1, 51, 0, 6, 2, 6, 50, 1, 34, 2, 0, 1
end table

function ARZSetup_Function97
	object.value6 = temp1
	object.value6 >>= 16
	object.value7 = temp1
	object.value7 >>= 8
	object.value7 &= 255
	object.value8 = temp1
	object.value8 &= 255
	object.value9 = temp2
	object.value9 >>= 16
	object.value10 = temp2
	object.value10 >>= 8
	object.value10 &= 255
	object.value11 = temp2
	object.value11 &= 255
	if object.value9 == 0
		object.value9 = 1
	end if
	if object.value10 == 0
		object.value10 = 1
	end if
	if object.value11 == 0
		object.value11 = 1
	end if
	object.value6 <<= 8
	object.value7 <<= 8
	object.value8 <<= 8
	object.value6 /= object.value9
	object.value7 /= object.value10
	object.value8 /= object.value11
	if object.value6 == 0
		object.value6 = 256
	end if
	if object.value7 == 0
		object.value7 = 256
	end if
	if object.value8 == 0
		object.value8 = 256
	end if
end function


function ARZSetup_Function98
	temp0 = 0
	GetPaletteEntry(1, 2, temp1)
	SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
	temp0++
	GetPaletteEntry(1, 3, temp1)
	SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
	temp0++
	GetPaletteEntry(1, 4, temp1)
	SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
	temp0++
	GetPaletteEntry(1, 5, temp1)
	SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
	temp0++
	while temp0 < 64
		GetPaletteEntry(0, 2, temp1)
		GetPaletteEntry(1, 2, temp2)
		CallFunction(ARZSetup_Function97)
		GetTableValue(temp1, temp0, PlayerObject_SonicSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 224
		temp1 /= object.value6
		temp2 *= 224
		temp2 /= object.value7
		temp3 *= 224
		temp3 /= object.value8
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
		temp0++
		GetPaletteEntry(0, 3, temp1)
		GetPaletteEntry(1, 3, temp2)
		CallFunction(ARZSetup_Function97)
		GetTableValue(temp1, temp0, PlayerObject_SonicSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 232
		temp1 /= object.value6
		temp2 *= 232
		temp2 /= object.value7
		temp3 *= 232
		temp3 /= object.value8
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
		temp0++
		GetPaletteEntry(0, 4, temp1)
		GetPaletteEntry(1, 4, temp2)
		CallFunction(ARZSetup_Function97)
		GetTableValue(temp1, temp0, PlayerObject_SonicSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 244
		temp1 /= object.value6
		temp2 *= 244
		temp2 /= object.value7
		temp3 *= 244
		temp3 /= object.value8
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
		temp0++
		GetPaletteEntry(0, 5, temp1)
		GetPaletteEntry(1, 5, temp2)
		CallFunction(ARZSetup_Function97)
		GetTableValue(temp1, temp0, PlayerObject_SonicSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 256
		temp1 /= object.value6
		temp2 *= 256
		temp2 /= object.value7
		temp3 *= 256
		temp3 /= object.value8
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
		temp0++
	loop
end function


function ARZSetup_Function99
	temp0 = 0
	GetPaletteEntry(1, 13, temp1)
	SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
	temp0++
	GetPaletteEntry(1, 17, temp1)
	SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
	temp0++
	GetPaletteEntry(1, 18, temp1)
	SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
	temp0++
	GetPaletteEntry(1, 16, temp1)
	SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
	temp0++
	while temp0 < 24
		GetPaletteEntry(0, 13, temp1)
		GetPaletteEntry(1, 13, temp2)
		CallFunction(ARZSetup_Function97)
		GetTableValue(temp1, temp0, PlayerObject_TailsSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 248
		temp1 /= object.value6
		temp2 *= 248
		temp2 /= object.value7
		temp3 *= 248
		temp3 /= object.value8
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
		temp0++
		GetPaletteEntry(0, 17, temp1)
		GetPaletteEntry(1, 17, temp2)
		CallFunction(ARZSetup_Function97)
		GetTableValue(temp1, temp0, PlayerObject_TailsSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 256
		temp1 /= object.value6
		temp2 *= 256
		temp2 /= object.value7
		temp3 *= 256
		temp3 /= object.value8
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
		temp0++
		GetPaletteEntry(0, 18, temp1)
		GetPaletteEntry(1, 18, temp2)
		CallFunction(ARZSetup_Function97)
		GetTableValue(temp1, temp0, PlayerObject_TailsSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 256
		temp1 /= object.value6
		temp2 *= 256
		temp2 /= object.value7
		temp3 *= 256
		temp3 /= object.value8
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
		temp0++
		GetPaletteEntry(0, 16, temp1)
		GetPaletteEntry(1, 16, temp2)
		CallFunction(ARZSetup_Function97)
		GetTableValue(temp1, temp0, PlayerObject_TailsSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 256
		temp1 /= object.value6
		temp2 *= 256
		temp2 /= object.value7
		temp3 *= 256
		temp3 /= object.value8
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
		temp0++
	loop
end function


function ARZSetup_Function100
	temp0 = 0
	if saved.knuckstyle == 2
		temp6 = PlayerObject_KnuxS2SuperPal
		temp7 = PlayerObject_KnuxS2SuperPalAlt
	else
		temp6 = PlayerObject_KnuxSuperPal
		temp7 = PlayerObject_KnuxSuperAltPal
	end if
	GetPaletteEntry(1, 26, temp1)
	SetTableValue(temp1, temp0, temp7)
	temp0++
	GetPaletteEntry(1, 27, temp1)
	SetTableValue(temp1, temp0, temp7)
	temp0++
	GetPaletteEntry(1, 28, temp1)
	SetTableValue(temp1, temp0, temp7)
	temp0++
end function


function ARZSetup_Function101
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			SwapMusicTrack("AquaticRuin_F.ogg", 0, 1, 0x1F40)
			break
		case 2
			SetMusicTrack("AquaticRuin_F.ogg", 0, 1)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x78B1, 0x1F40)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("AquaticRuin_F.ogg", 0, 1)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function ARZSetup_Function102
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			SwapMusicTrack("AquaticRuin.ogg", 0, 1, 0x30D4)
			break
		case 2
			SetMusicTrack("AquaticRuin.ogg", 0, 1)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9717, 0x30D4)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("AquaticRuin.ogg", 0, 1)
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function


function ARZSetup_Function103
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			SwapMusicTrack("AquaticRuinAct2_F.ogg", 0, 0x2163, 0x1F40)
			break
		case 2
			SetMusicTrack("AquaticRuinAct2_F.ogg", 0, 0x2163)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x78B1, 0x1F40)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("AquaticRuinAct2_F.ogg", 0, 0x2163)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function ARZSetup_Function104
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			SwapMusicTrack("AquaticRuinAct2.ogg", 0, 0x288B, 0x30D4)
			break
		case 2
			SetMusicTrack("AquaticRuinAct2.ogg", 0, 0x288B)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9717, 0x30D4)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("AquaticRuinAct2.ogg", 0, 0x288B)
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function


function ARZSetup_Function105
	temp0 = 0
		temp3 = PlayerObject_SonicSuperPal
		temp2 = PlayerObject_MetalS2SuperPalAlt
	temp0 = 0
	GetPaletteEntry(1, 64, temp1)
	SetTableValue(temp1, temp0, temp5)
	temp0++
	GetPaletteEntry(1, 65, temp1)
	SetTableValue(temp1, temp0, temp5)
	temp0++
	GetPaletteEntry(1, 66, temp1)
	SetTableValue(temp1, temp0, temp5)
	temp0++
	GetPaletteEntry(1, 67, temp1)
	SetTableValue(temp1, temp0, temp5)
end function


event ObjectMain
	object.value1++
	if object.value1 == 6
		object.value1 = 0
		RotatePalette(0, 162, 165, 1)
		RotatePalette(1, 162, 165, 1)
	end if
	if ARZSetup_value33 > 0
		ARZSetup_value33--
	end if
end event


event ObjectDraw
	temp0 = stage.waterLevel
	temp0 -= screen.yoffset
	if temp0 < 0
		temp0 = 0
	end if
	if temp0 > screen.ysize
		temp0 = screen.ysize
	end if
	SetActivePalette(0, 0, temp0)
	if ARZSetup_value33 > 0
		SetActivePalette(2, temp0, screen.ysize)
	else
		SetActivePalette(1, temp0, screen.ysize)
	end if
end event


event ObjectStartup
	stage.midPoint = 2
	SetPaletteEntry(0, 192, 0)
	if saved.act2tracks == 1
		if stage.actNum == 1
			SetMusicTrack("AquaticRuin.ogg", 0, 1)
			SpeedUpMusic = ARZSetup_Function101
			SlowDownMusic = ARZSetup_Function102
		end if
		if stage.actNum == 2
			SetMusicTrack("AquaticRuinAct2.ogg", 0, 0x288B)
			SpeedUpMusic = ARZSetup_Function103
			SlowDownMusic = ARZSetup_Function104
		end if	
	else
		SetMusicTrack("AquaticRuin.ogg", 0, 1)
		SpeedUpMusic = ARZSetup_Function101
		SlowDownMusic = ARZSetup_Function102
	end if
	SpeedUpMusic = ARZSetup_Function101
	SlowDownMusic = ARZSetup_Function102
	animalType1 = TypeName[Flicky]
	animalType2 = TypeName[Pecky]
	object[10].type = TypeName[ARZ Setup]
	object[10].priority = PRIORITY_ACTIVE
	object[10].drawOrder = 0
	
	switch stage.actNum
	case 1
		stage.curYBoundary1 = 512
		tileLayer[1].scrollPos = -0x1800000
		tileLayer[2].scrollPos = -0x1800000
		tileLayer[1].parallaxFactor = 256
		tileLayer[2].parallaxFactor = 256
		
		temp0 = 7906
		temp0 <<= 16
		temp1 = 1040
		temp1 <<= 16
		CreateTempObject(TypeName[Plane Sw H], 6, temp0, temp1)
		
		temp0 = object[tempObjectPos].propertyValue
		temp0 &= 3
		temp0 += 2
		object[tempObjectPos].value0 = 1
		object[tempObjectPos].value0 <<= temp0
		object[tempObjectPos].value0 <<= 3
		object[tempObjectPos].value1 = object[tempObjectPos].value0
		FlipSign(object[tempObjectPos].value0)
		GetBit(object[tempObjectPos].value2, object[tempObjectPos].propertyValue, 2)
		GetBit(object[tempObjectPos].value3, object[tempObjectPos].propertyValue, 3)
		GetBit(object[tempObjectPos].value4, object[tempObjectPos].propertyValue, 4)
		object[tempObjectPos].value4 += 4
		GetBit(object[tempObjectPos].value5, object[tempObjectPos].propertyValue, 5)
		object[tempObjectPos].value5 += 4
		GetBit(object[tempObjectPos].value6, object[tempObjectPos].propertyValue, 7)
		
		break
	case 2
		stage.curYBoundary1 = 384
		tileLayer[3].scrollPos = -0x800000
		tileLayer[4].scrollPos = -0x800000
		tileLayer[3].parallaxFactor = 128
		tileLayer[4].parallaxFactor = 128
		
		object[304].xpos += 0x30 0000
		object[304].ypos += 0x1E 0000
		object[295].xpos -= 0x50000
		ResetObjectEntity(495, TypeName[Blank Object], 0, 0, 0)
		break
	end switch
	
	LoadPalette("ARZ_WaterPal.act", 1, 0, 0, 256)
	CallFunction(ARZSetup_Function98)
	CallFunction(ARZSetup_Function99)
	CallFunction(ARZSetup_Function100)
	CallFunction(ARZSetup_Function105)
	LoadPalette("ElectricFlash.act", 2, 0, 0, 256)
	ARZSetup_value33 = 0
	if options.attractMode == 1
		switch stage.playerListPos
		case 0
			PlayerObject_ReplayTable = ARZSetup_table0
			PlayerObject_ReplaySize = 154
			PlayerObject_ReplayLength = 0x6CC
			break
		case 1
			PlayerObject_ReplayTable = ARZSetup_table1
			PlayerObject_ReplaySize = 138
			PlayerObject_ReplayLength = 0x708
			break
		case 2
			PlayerObject_ReplayTable = ARZSetup_table2
			PlayerObject_ReplaySize = 126
			PlayerObject_ReplayLength = 0x708
			break
		end switch
		CallFunction(PlayerObject_InitReplay)
	end if
	switch stage.actNum
		case 0
		case 1
			//LoadPalette("ARZAct1Pal.act", 0, 128, 128, 256)
			break
		case 2
			LoadPalette("ARZAct2Pal.act", 0, 128, 128, 256)
			break
	end switch
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
